package com.canaan.business.service.basic.impl;

import java.util.Date;
import java.util.List;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.basic.AddressMapper;
import com.canaan.business.domain.basic.Address;
import com.canaan.business.service.basic.IAddressService;
import com.canaan.common.annotation.TableName;
/**
 * 地址信息Service业务层处理
 *
 * @author canaan
 * @date 2025-03-24
 */
@Service
public class AddressServiceImpl implements IAddressService {
    @Resource
    private AddressMapper addressMapper;

    /**
     * 查询地址信息
     *
     * @param id 地址信息主键
     * @return 地址信息
     */
    @Override
    public Address selectAddressById(Long id) {
        return addressMapper.selectAddressById(id);
    }

    /**
     * 查询地址信息列表
     *
     * @param address 地址信息
     * @return 地址信息
     */
    @Override
    public List<Address> selectAddressList(Address address) {
        return addressMapper.selectAddressList(address);
    }

    /**
     * 新增地址信息
     *
     * @param address 地址信息
     * @return 结果
     */
    @Override
    public int insertAddress(Address address) {
        if(StringUtils.isEmpty(address.getCreateBy())) {
            address.setCreateBy(SecurityUtils.getUsername());
        }
        address.setCreateTime(DateUtils.getNowDate());
        return addressMapper.insertAddress(address);
    }

    /**
     * 批量新增地址信息
     *
     * @param addressList 地址信息列表
     * @return 结果
     */
    @Override
    public int batchInsertAddress(List<Address> addressList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (Address address : addressList){
            address.setCreateBy(username);
            address.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<addressList.size();) {
            int endIndex = i+500;
            if (endIndex > addressList.size()) {
                endIndex = addressList.size();
            }
            insertNum = insertNum + addressMapper.batchInsertAddress(addressList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改地址信息
     *
     * @param address 地址信息
     * @return 结果
     */
    @Override
    public int updateAddress(Address address) {
        if(StringUtils.isEmpty(address.getUpdateBy())) {
            address.setUpdateBy(SecurityUtils.getUsername());
        }
        address.setUpdateTime(DateUtils.getNowDate());
        return addressMapper.updateAddress(address);
    }

    /**
     * 批量修改地址信息
     *
     * @param addressList 地址信息列表
     * @return 结果
     */
    @Override
    public int batchUpdateAddress(List<Address> addressList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (Address address : addressList){
            address.setUpdateBy(username);
            address.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<addressList.size();) {
            int endIndex = i+500;
            if (endIndex > addressList.size()) {
                endIndex = addressList.size();
            }
            updateNum = updateNum + addressMapper.batchUpdateAddress(addressList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除地址信息
     *
     * @param ids 需要删除的地址信息主键集合
     * @return 结果
     */
    @Override
    public int deleteAddressByIds(Long[] ids) {
        return addressMapper.deleteAddressByIds(ids);
    }

    /**
     * 删除地址信息信息
     *
     * @param id 地址信息主键
     * @return 结果
     */
    @Override
    public int deleteAddressById(Long id) {
        return addressMapper.deleteAddressById(id);
    }
}
